package org.zsk.cs.sendsms;

import com.alibaba.druid.support.json.JSONUtils;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *     阿里短信服务请求工具类
 * </p>
 * @author zsk
 * @date 2020/7/18 上午11:33
 */
@Slf4j
public class AliYunRequestUtils {

    /**
     * 客户端实例
     */
    public static final IAcsClient CLIENT=new DefaultAcsClient(
            DefaultProfile.getProfile(AliYunParameter.REGION_ID,
                    AliYunParameter.ACCESS_KEY_ID,
                    AliYunParameter.ACCESS_SECRET));

    /**
     * 添加模板
     * @param req
     * @return
     */
    public static SmsTemplateRespDto addSmsTemplate(SmsTemplateReqDto req){
        log.debug("添加模板请求参数:{}",req);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(AliYunParameter.DOMAIN);
        request.setSysVersion(AliYunParameter.VERSION);
        request.setSysAction(AliYunSmsAction.ADD_SMS_TEMPLATE);
        request.putQueryParameter("RegionId", AliYunParameter.REGION_ID);
        //短信类型
//        0：验证码。
//        1：短信通知。
//        2：推广短信。
//        3：国际/港澳台消息。
        request.putQueryParameter("TemplateType", req.getTemplateType().toString());
        //模板名称
        request.putQueryParameter("TemplateName", req.getTemplateName());
        //模板内容
        request.putQueryParameter("TemplateContent", req.getTemplateContent());
        //短信模板申请说明
        request.putQueryParameter("Remark", req.getRemark());
        try {
            CommonResponse response = AliYunRequestUtils.CLIENT.getCommonResponse(request);
            return (SmsTemplateRespDto) JSONUtils.parse(response.getData());
        } catch (ClientException e) {
            log.error("添加模板请求失败:{}",e.getMessage());
            return null;
        }
    }

    /**
     * 修改模板
     * @param req
     * @return
     */
    public static SmsTemplateRespDto modifySmsTemplate(SmsTemplateReqDto req){
        log.debug("修改模板请求参数:{}",req);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(AliYunParameter.DOMAIN);
        request.setSysVersion(AliYunParameter.VERSION);
        request.setSysAction(AliYunSmsAction.MODIFY_SMS_TEMPLATE);
        request.putQueryParameter("RegionId", AliYunParameter.REGION_ID);
        //短信类型
//        0：验证码。
//        1：短信通知。
//        2：推广短信。
//        3：国际/港澳台消息。
        request.putQueryParameter("TemplateType", req.getTemplateType().toString());
        //模板名称
        request.putQueryParameter("TemplateName", req.getTemplateName());
        //模板内容
        request.putQueryParameter("TemplateContent", req.getTemplateContent());
        //短信模板申请说明
        request.putQueryParameter("Remark", req.getRemark());
        //短信模板CODE
        request.putQueryParameter("TemplateCode", req.getTemplateCode());
        try {
            CommonResponse response = AliYunRequestUtils.CLIENT.getCommonResponse(request);
            return (SmsTemplateRespDto) JSONUtils.parse(response.getData());
        } catch (ClientException e) {
            log.error("修改模板请求失败{}:",e.getMessage());
            return null;
        }
    }

    /**
     * 查询模板
     * @param req
     * @return
     */
    public static SmsTemplateRespDto querySmsTemplate(SmsTemplateReqDto req){
        log.debug("查询模板请求参数:{}",req);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(AliYunParameter.DOMAIN);
        request.setSysVersion(AliYunParameter.VERSION);
        request.setSysAction(AliYunSmsAction.QUERY_SMS_TEMPLATE);
        request.putQueryParameter("RegionId", AliYunParameter.REGION_ID);
        //短信模板CODE
        request.putQueryParameter("TemplateCode", req.getTemplateCode());
        try {
            CommonResponse response = AliYunRequestUtils.CLIENT.getCommonResponse(request);
            return (SmsTemplateRespDto) JSONUtils.parse(response.getData());
        } catch (ClientException e) {
            log.error("查询模板请求失败{}:",e.getMessage());
            return null;
        }
    }

    /**
     * 删除模板
     * @param req
     * @return
     */
    public static SmsTemplateRespDto deleteSmsTemplate(SmsTemplateReqDto req){
        log.debug("删除模板请求参数:{}",req);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(AliYunParameter.DOMAIN);
        request.setSysVersion(AliYunParameter.VERSION);
        request.setSysAction(AliYunSmsAction.DELETE_SMS_TEMPLATE);
        request.putQueryParameter("RegionId", AliYunParameter.REGION_ID);
        //短信模板CODE
        request.putQueryParameter("TemplateCode", req.getTemplateCode());
        try {
            CommonResponse response = AliYunRequestUtils.CLIENT.getCommonResponse(request);
            return (SmsTemplateRespDto) JSONUtils.parse(response.getData());
        } catch (ClientException e) {
            log.error("删除模板请求失败{}:",e.getMessage());
            return null;
        }
    }

    /**
     * 添加签名
     * @param req
     * @return
     */
    public static SmsSignRespDto addSmsSign(SmsSignReqDto req){
        log.debug("添加签名请求参数:{}",req);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(AliYunParameter.DOMAIN);
        request.setSysVersion(AliYunParameter.VERSION);
        request.setSysAction(AliYunSmsAction.ADD_SMS_SIGN);
        request.putQueryParameter("RegionId", AliYunParameter.REGION_ID);
        request.putQueryParameter("SignName", req.getSignName());
        request.putQueryParameter("SignSource", req.getSignSource().toString());
        request.putQueryParameter("Remark", req.getRemark());
        try {
            CommonResponse response = AliYunRequestUtils.CLIENT.getCommonResponse(request);
            return (SmsSignRespDto) JSONUtils.parse(response.getData());
        } catch (ClientException e) {
            log.error("添加签名请求失败{}:",e.getMessage());
            return null;
        }
    }

    /**
     * 修改签名
     * @param req
     * @return
     */
    public static SmsSignRespDto modifySmsSign(SmsSignReqDto req){
        log.debug("修改签名请求参数:{}",req);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(AliYunParameter.DOMAIN);
        request.setSysVersion(AliYunParameter.VERSION);
        request.setSysAction(AliYunSmsAction.MODIFY_SMS_SIGN);
        request.putQueryParameter("RegionId", AliYunParameter.REGION_ID);
        request.putQueryParameter("SignName", req.getSignName());
        request.putQueryParameter("SignSource", req.getSignSource().toString());
        request.putQueryParameter("Remark", req.getRemark());
        try {
            CommonResponse response = AliYunRequestUtils.CLIENT.getCommonResponse(request);
            return (SmsSignRespDto) JSONUtils.parse(response.getData());
        } catch (ClientException e) {
            log.error("修改签名请求失败{}:",e.getMessage());
            return null;
        }
    }

    /**
     * 查询签名
     * @param req
     * @return
     */
    public static SmsSignRespDto querySmsSign(SmsSignReqDto req){
        log.debug("查询签名请求参数:{}",req);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(AliYunParameter.DOMAIN);
        request.setSysVersion(AliYunParameter.VERSION);
        request.setSysAction(AliYunSmsAction.QUERY_SMS_SIGN);
        request.putQueryParameter("RegionId", AliYunParameter.REGION_ID);
        request.putQueryParameter("SignName", req.getSignName());
        try {
            CommonResponse response = AliYunRequestUtils.CLIENT.getCommonResponse(request);
            return (SmsSignRespDto) JSONUtils.parse(response.getData());
        } catch (ClientException e) {
            log.error("查询签名请求失败{}:",e.getMessage());
            return null;
        }
    }

    /**
     * 删除签名
     * @param req
     * @return
     */
    public static SmsSignRespDto deleteSmsSign(SmsSignReqDto req){
        log.debug("删除签名请求参数:{}",req);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(AliYunParameter.DOMAIN);
        request.setSysVersion(AliYunParameter.VERSION);
        request.setSysAction(AliYunSmsAction.DELETE_SMS_SIGN);
        request.putQueryParameter("RegionId", AliYunParameter.REGION_ID);
        request.putQueryParameter("SignName", req.getSignName());
        try {
            CommonResponse response = AliYunRequestUtils.CLIENT.getCommonResponse(request);
            return (SmsSignRespDto) JSONUtils.parse(response.getData());
        } catch (ClientException e) {
            log.error("删除签名请求失败{}:",e.getMessage());
            return null;
        }
    }

    /**
     * 查看短信发送记录和发送状态
     * @param req
     * @return
     */
    public static SmsQuerySendDetailsRespDto querySendDetails(SmsQuerySendDetailsReqDto req){
        log.debug("查看短信发送记录和发送状态请求参数:{}",req);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(AliYunParameter.DOMAIN);
        request.setSysVersion(AliYunParameter.VERSION);
        request.setSysAction(AliYunSmsAction.QUERY_SEND_DETAILS);
        request.putQueryParameter("RegionId", AliYunParameter.REGION_ID);
        request.putQueryParameter("PhoneNumber", req.getPhoneNumber());
        request.putQueryParameter("SendDate", req.getSendDate());
        request.putQueryParameter("PageSize", req.getPageSize().toString());
        request.putQueryParameter("CurrentPage", req.getCurrentPage().toString());
        try {
            CommonResponse response = AliYunRequestUtils.CLIENT.getCommonResponse(request);
            return (SmsQuerySendDetailsRespDto) JSONUtils.parse(response.getData());
        } catch (ClientException e) {
            log.error("查看短信发送记录和发送状态请求失败{}:",e.getMessage());
            return null;
        }
    }

    /**
     * 批量发送短信
     * @param req
     * @return
     */
    public static SmsSendBatchRespDto sendBatchSms(SmsSendBatchReqDto req){
        log.debug("批量发送短信请求参数:{}",req);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(AliYunParameter.DOMAIN);
        request.setSysVersion(AliYunParameter.VERSION);
        request.setSysAction(AliYunSmsAction.SEND_BATCH_SMS);
        request.putQueryParameter("RegionId", AliYunParameter.REGION_ID);
        request.putQueryParameter("PhoneNumberJson", req.getPhoneNumberJson());
        request.putQueryParameter("SignNameJson", req.getSignNameJson());
        request.putQueryParameter("TemplateCode", req.getTemplateCode());
//        request.putQueryParameter("TemplateParamJson", req.getTemplateParamJson());
//        request.putQueryParameter("SmsUpExtendCodeJson", req.getSmsUpExtendCodeJson());
        try {
            CommonResponse response = AliYunRequestUtils.CLIENT.getCommonResponse(request);
            return (SmsSendBatchRespDto) JSONUtils.parse(response.getData());
        } catch (ClientException e) {
            log.error("批量发送短信请求失败{}:",e.getMessage());
            return null;
        }
    }

    /**
     * 发送短信
     * @param req
     * @return
     */
    public static SmsSendRespDto sendBatchSms(SmsSendReqDto req){
        log.debug("发送短信请求参数:{}",req);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(AliYunParameter.DOMAIN);
        request.setSysVersion(AliYunParameter.VERSION);
        request.setSysAction(AliYunSmsAction.SEND_SMS);
        request.putQueryParameter("RegionId", AliYunParameter.REGION_ID);
        request.putQueryParameter("PhoneNumbers", req.getPhoneNumbers());
        request.putQueryParameter("SignName", req.getSignName());
        request.putQueryParameter("TemplateCode", req.getTemplateCode());
//        request.putQueryParameter("TemplateParam", req.getTemplateParam());
//        request.putQueryParameter("SmsUpExtendCode", req.getSmsUpExtendCode());
//        request.putQueryParameter("OutId", req.getOutId());
        try {
            CommonResponse response = AliYunRequestUtils.CLIENT.getCommonResponse(request);
            return (SmsSendRespDto) JSONUtils.parse(response.getData());
        } catch (ClientException e) {
            log.error("发送短信请求失败{}:",e.getMessage());
            return null;
        }
    }

}
